﻿Ext.extend(NExt.form.FormPanel, Ext.form.FormPanel, {
    getModel: function () {
        var model = {};
        this.items.each(function (field) {
            this.getFieldModel(field, model);
        }, this);

        return model;
    },

    getFieldModel: function (field, model) {
        var isGetModelFnDefined = Ext.isDefined(field.getModel);
        var fieldHasItems = Ext.isDefined(field.items);
        var isContainerField = (!isGetModelFnDefined) && (fieldHasItems);
        if (isContainerField) {
            field.items.each(function (innerField) {
                this.getFieldModel(innerField, model);
            }, this);

            return;
        }

        if (Ext.isEmpty(field.dataMember)) {
            return;
        }

        var fieldModel = field.getModel();
        this.setDataMemberValue(model, field.dataMember, fieldModel);
    },

    setModel: function (model) {
        if (!this.initialModel) {
            this.initialModel = Ext.apply({}, model);
        }

        this.items.each(function (field) {
            this.setFieldModel(field, model);
        }, this);
    },

    setFieldModel: function (field, model) {
        var isSetModelFnDefined = Ext.isDefined(field.setModel);
        var fieldHasItems = Ext.isDefined(field.items);
        var isContainerField = (!isSetModelFnDefined) && (fieldHasItems);
        if (isContainerField) {
            field.items.each(function (innerField) {
                this.setFieldModel(innerField, model);
            }, this);

            return;
        }

        if (Ext.isEmpty(field.dataMember)) {
            return;
        }

        var fieldModel = this.getDataMemberValue(model, field.dataMember);
        if (Ext.isDefined(fieldModel)) {
            field.setModel(fieldModel);
        }
    },

    getDataMemberValue: function (model, dataMember) {
        var value;
        var execStr = "value = model";
        var parts = dataMember.split('.');
        Ext.each(parts, function (part) {
            execStr += "['" + part + "']";
        });
        eval(execStr);

        return value;
    },

    setDataMemberValue: function (model, dataMember, value) {
        var execStr = "model";
        var parts = dataMember.split('.');
        Ext.each(parts, function (part) {
            execStr += "['" + part + "']";
            var str = "if (!Ext.isDefined(" + execStr + ")) " + execStr + " = {}"
            eval(str);
        });
        execStr += " = value";
        eval(execStr);
    },

    isDirty: function () {
        var currentModel = this.getModel();
        var dirty = this.checkDirty(currentModel, this.initialModel);
        return dirty;
    },

    checkDirty: function (model1, model2) {
        for (var prop in model1) {
            var currentPropValue = model1[prop];
            var initialPropValue = model2[prop];

            //todo : isDate kontrolu kaldirilmali..
            if (Ext.isFunction(currentPropValue) || Ext.isDate(currentPropValue)) {
                continue;
            }

            if ((currentPropValue == '' || currentPropValue == undefined) && (initialPropValue == '' || initialPropValue == undefined)) {
                continue;
            }

            if (Ext.isDefined(currentPropValue) && !Ext.isDefined(initialPropValue)) {
                return true;
            }

            if (Ext.isObject(currentPropValue)) {
                var dirty = this.checkDirty(currentPropValue, initialPropValue);
                if (dirty) {
                    return true;
                }

                continue;
            }

            //todo : bu kontrol kaldirilmali..
            if (Ext.isNumber(currentPropValue) && Ext.isNumber(initialPropValue)) {
                currentPropValue = currentPropValue.toFixed(2);
                initialPropValue = initialPropValue.toFixed(2);
            }

            if (currentPropValue != initialPropValue) {
                return true;
            }
        }

        return false;
    }
});